package com.order.service.impl;

import com.order.bean.OrderDetail;
import com.order.common.util.SqlSessionFactoryUtil;
import com.order.mapper.FoodsMapper;
import com.order.mapper.OrderDetailMapper;
import com.order.service.OrderDetailService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class OrderDetailServiceImpl implements OrderDetailService {

    private OrderDetailMapper orderDetailMapper;

    private SqlSession sqlSession;

    private void closeMapper() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    private void initMapper() {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();
        SqlSession sqlSession = factory.openSession(true);
        orderDetailMapper = sqlSession.getMapper(OrderDetailMapper.class);
    }

    @Override
    public OrderDetail queryById(Integer id) {
        initMapper();
        OrderDetail orderDetail = orderDetailMapper.queryById(id);
        closeMapper();
        return orderDetail;
    }

    @Override
    public List<OrderDetail> queryAll(OrderDetail orderDetail) {
        initMapper();
        List<OrderDetail> orderDetails = orderDetailMapper.queryAll(orderDetail);
        closeMapper();
        return orderDetails;
    }

    @Override
    public OrderDetail insert(OrderDetail orderDetail) {
        initMapper();
        orderDetailMapper.insert(orderDetail);
        closeMapper();
        return orderDetail;
    }

    @Override
    public OrderDetail update(OrderDetail orderDetail) {
        initMapper();
        orderDetailMapper.update(orderDetail);
        closeMapper();
        return orderDetail;
    }

    @Override
    public boolean deleteById(Integer id) {
        initMapper();
        boolean flag = orderDetailMapper.deleteById(id);
        closeMapper();
        return flag;
    }
}
